﻿using UnityEngine;
using System.Collections;

namespace View
{
    public class ShokwaveParticle
    {

        private int seed;
        private float radius;
        private float maxRadius = 0.5f;
        private float minRadius = 0.2f;
        private Vector2 startPos;
        private Vector2 position;

        private Color fadeColor;
        private Color previousColor;

        private float lifeTime;
        private float maxLifeTime;
        private float timeElapsed;

        private Texture2D texture;

        public ShokwaveParticle(Vector2 startPos, float lifeTime, float ratio)
        {
            this.startPos = startPos;
            this.lifeTime = lifeTime;
            this.maxRadius *= ratio;
            this.minRadius *= ratio;

            fadeColor = GUI.color;
            previousColor = GUI.color;

            setUpParticle();
        }

        private void setUpParticle()
        {
            lifeTime = 0.3f;

            this.position = new Vector2(startPos.x, startPos.y);
        }

        private void interpolation()
        {
            float startValue = 1.0f;
            float endValue = 0.0f;

            float t = timeElapsed / lifeTime;

            if (t > startValue)
            {
                timeElapsed = 1.0f;
                //setUpParticle();
            }
            else
            {
                radius = minRadius + t * maxRadius;
                fadeColor.a = (endValue * t + (1.0f - t) * startValue);
            }
        }

        // Update is called once per frame
        public void Update()
        {
            timeElapsed += Time.deltaTime;
        }

        public void OnGUI(GameCamera cam, Texture2D texture)
        {
            interpolation();

            Vector2 visualPos = cam.getVisualCoordinates(position);
            Vector2 visualSize = cam.getVisualSize(radius * 2, radius * 2);

            GUI.color = fadeColor;
            GUI.DrawTexture(new Rect(visualPos.x - visualSize.x / 2, visualPos.y - visualSize.y / 2, visualSize.x, visualSize.y), texture);
            GUI.color = previousColor;
        }
    }
}